package com.springboot.erp.service.impl;


import com.springboot.erp.entity.Return;
import com.springboot.erp.mapper.ReturnMapper;
import com.springboot.erp.service.ReturnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
/**
 * 退货服务实现类
 */
public class ReturnServiceImpl implements ReturnService {

    @Resource
    private ReturnMapper returnMapper;

    /**
     * 创建退货单
     */
    @Override
    public boolean createReturn(Return returnOrder) {
        // 插入退货单
        int returnResult = returnMapper.insertReturn(returnOrder);
        if (returnResult > 0) {
            // 插入退货单明细
            int itemResult = returnMapper.insertReturnItem(returnOrder);
            return itemResult > 0;
        }
        return false;
    }

    /**
     * 查询退货单
     */
    @Override
    public Return selectReturnById(Long id) {
        return returnMapper.selectReturnList(id);
    }

    /**
     * 修改退货单
     */
    @Override
    public boolean updateReturn(Return returnOrder) {
        // 更新退货单
        int returnResult = returnMapper.updateReturnById(returnOrder);
        if (returnResult > 0) {
            // 更新退货单明细
            int itemResult = returnMapper.updateReturnItem(returnOrder);
            return itemResult > 0;
        }
        return false;
    }

    /**
     * 删除退货单
     */
    @Override
    public boolean deleteReturn(Long id) {
        // 先删除子表数据
        int itemResult = returnMapper.deleteReturnItemByHeaderId(id);
        // 再删除主表数据
        int returnResult = returnMapper.deleteReturnById(id);
        return returnResult > 0;
    }
}